package com.controller;

import com.model.GroupAll;
import com.model.ResAll;
import com.model.ResAllExample;
import com.service.RestaurantService;
import com.util.FileUpload;
import com.util.WriteJSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Hu on 2017/3/25.
 */
@Controller
@RequestMapping(value = "/cg/")
public class RestController {
    @Autowired
    RestaurantService restaurantService;

    /*
    * 测试页面
    * */
    @RequestMapping(value = "test")
    public String test() {
        return "home";
    }

    /*
    * 新增机构
    * */
    @RequestMapping(value = "add_group")
    public
    @ResponseBody
    Map<String, Object> add_group(
            @RequestParam(value = "group_name") String name,
            @RequestParam(value = "group_admin") String admin) {
        GroupAll groupAll = new GroupAll();
        groupAll.setGroupName(name);
        groupAll.setGroupAdmin(admin);
        return WriteJSONUtil.writeJSON(restaurantService.insert(groupAll));
    }

    /*
    * 新增餐馆
    */

    @RequestMapping(value = "add_res")
    public
    @ResponseBody
    Map<String, Object> add_res(
            @RequestParam(value = "res_name") String name,
            @RequestParam(value = "res_address") String address,
            @RequestParam(value = "group_id") Integer group_id,
            @RequestParam(value = "res_phone") String phone) {
        ResAll resAll = new ResAll();
        resAll.setResName(name);
        resAll.setResAddress(address);
        resAll.setResPhone(phone);
        resAll.setGroupId(group_id);

        return WriteJSONUtil.writeJSON(restaurantService.insert(resAll));
    }

    /*
    * 获取所有餐馆的信息
    *
    * */
    @RequestMapping(value = "getResses")
    public
    @ResponseBody
    Map<String, Object> getResses(ResAllExample resAllExample) {
        return WriteJSONUtil.writeJSON(restaurantService.selectByExample(resAllExample));
    }


    /*
    * 获取所有的餐馆 没有请求头的
    * */
    @RequestMapping(value = "getRes")
    public
    @ResponseBody
    Map<String, Object> getRes(@RequestParam(value = "id") Integer id) throws Exception {
        ResAll items = restaurantService.getRestById(id);
        return WriteJSONUtil.writeJSON(items);
    }

    /*
    *  更新餐馆信息
    * */
    @RequestMapping(value = "updateRes")
    public
    @ResponseBody
    Map<String, Object> updateRes(
            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "res_name") String name,
            @RequestParam(value = "res_address") String address,
            @RequestParam(value = "group_id") Integer group_id,
            @RequestParam(value = "res_phone") String phone
    ) {
        ResAll resAll = new ResAll();
        resAll.setId(id);
        resAll.setResName(name);
        resAll.setResAddress(address);
        resAll.setResPhone(phone);
        resAll.setGroupId(group_id);
        return WriteJSONUtil.writeJSON(restaurantService.updateRes(resAll));
    }

    /*
    * 删除餐馆
    *
    * */
    @RequestMapping(value = "delRes")
    public
    @ResponseBody
    Map<String, Object> delRes(@RequestParam(value = "id") Integer id) {
        return WriteJSONUtil.writeJSON(restaurantService.delResById(id));
    }

    /*
     * 采用file.Transto 来保存上传的文件
     */
    @RequestMapping("upload")
    public
    @ResponseBody
    Map<String, Object> upload(@RequestParam("image") CommonsMultipartFile file) {
        String path = "E:/" + new Date().getTime() + file.getOriginalFilename();
        if (file.isEmpty()) {
            return WriteJSONUtil.writeJSON(0);
        } else {
            File newFile = new File(path);
            //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
            try {
                file.transferTo(newFile);
            } catch (IOException e) {
                e.printStackTrace();
                return WriteJSONUtil.writeJSON(0);
            }
            return WriteJSONUtil.writeJSON(1);
        }
    } /*
     * 采用file.Transto 来保存上传的文件
     */

    @RequestMapping("uploadWithValue")
    public
    @ResponseBody
    Map<String, Object> uploadWithValue(@RequestParam(value = "id") Integer id, @RequestParam("image") CommonsMultipartFile file) {
       //此路径必须存在webapp下 以便通过tomcat访问
        String path = "F:\\IdeaWorkSpace\\restaurant\\src\\main\\webapp\\images\\" + new Date().getTime() + file.getOriginalFilename();
        if (file.isEmpty()) {
            return WriteJSONUtil.writeJSON(0);
        } else {
            File newFile = new File(path);
            //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
            try {
                file.transferTo(newFile);
                ResAll resAll =new ResAll();
                resAll.setId(id);
                resAll.setResImage(path);
                restaurantService.updateRes(resAll);
            } catch (IOException e) {
                e.printStackTrace();
                return WriteJSONUtil.writeJSON(0);
            }
            return WriteJSONUtil.writeJSON(1);
        }
    }

    /*多文件上传*/
    @RequestMapping("uploadList")
    public
    @ResponseBody
    Map<String, Object> uploadList(@RequestParam(value = "res_name") String name,
                                   @RequestParam("image") List<CommonsMultipartFile> files) {
        for (CommonsMultipartFile file : files) {
            String path = "E:/" + new Date().getTime() + file.getOriginalFilename();
            if (file.isEmpty()) {
                return WriteJSONUtil.writeJSON(0);
            } else {
                File newFile = new File(path);
                //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
                try {
                    file.transferTo(newFile);
                } catch (IOException e) {
                    e.printStackTrace();
                    return WriteJSONUtil.writeJSON(0);
                }
                return WriteJSONUtil.writeJSON(name);
            }
        }
        return WriteJSONUtil.writeJSON(0);
    }


}
